﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp9
{
    internal class Clock
    {
        private int hour = 0;
        private int minute = 0;
        private int second = 0;
        public int Second
        {
            get => second;
            set
            {
                if (second != value) // 检查值是否改变
                {
                    second = value;
                    Tick(this); // 触发事件
                }
            }
        }
        public event TickHandler Tick;
        public event AlarmHandler Alarm;
        public Clock(int x,int y,int z)
        {
            if (x >= 0 && x < 24)
                hour = x;
            if (y >= 0 && y < 60)
                minute = y;
            if (y >= 0 && y < 60)
                second = z;
            Timer timer = new Timer(OnTimedEvent, null, 0, 1000);
            Tick += OnTick;
            Alarm += OnAlarm;
        }
        private void OnTimedEvent(object sender)
        {
            Second++;
            if (second == 60)
            {
                second = 0;
                minute++;
            }
            if (minute == 60)
            {
                minute = 0;
                hour++;
            }
            if (hour == 24)
            {
                hour = 0;
            }
        }
        void OnTick (object sender)
        {
            Console.WriteLine($"当前时间为：{hour}:{minute}:{second}");
        }
        void OnAlarm(object sender, TimeEventArgs arg)
        {
            while(true)
            {
                if (hour == arg.hour && minute == arg.minute && second == arg.second)
                {
                    Console.WriteLine("Alarm clock sound");
                    break;
                }
            }
        }
        public void SetAlarm(int x,int y,int z)
        {
            Console.WriteLine();
            TimeEventArgs arg = new TimeEventArgs() { hour = x, minute = y, second = z };
            Alarm(this, arg);
        }
    }
}
